package com.example.forkjoin;

import java.util.concurrent.RecursiveTask;

//RecursiveTask 有返回值的 forkjoin task 任务类
public class Task extends RecursiveTask<Integer> {

    private int segment = 100;//拆分的片
    private int start,end; //分片的开始结束

    public Task(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        if((end - start) > segment){

            int middle =( start + end)/2;
            Task task = new Task(start, middle);
            Task task1 = new Task(middle+1, end);
            task.fork();
            task1.fork();
            return task.join() + task1.join();

        }else{
            int j = 0;
            for(int i = start;i <=end; i++){
                j+=i;
            }
            return j;
        }
    }
}
